package SM::Server::Chat;

use warnings;
use strict;

use SM::Constants;

use Data::Dumper::Simple;

sub new {
	my ( $class, $arg_ref ) = @_;

	# Keys are channels, values are array refs of clients
	my %chat_rooms = (
		global => {
			members  => [],
			chat_log => [],
		},
	);

	my $self = bless { chat_rooms => \%chat_rooms, }, $class;

	return $self;
}

# Returns a packet and an array ref of recipients
sub channel_message {
	my ( $self, $message, $sender, $channel ) = @_;

	if ( exists $self->{chat_rooms}{$channel} ) {

		# Create packet and send it to all the clients in the chat room
		my $packet = SM::Server::Packet::Out::Chat::Message->new(
			{   reciever_type => CHANNEL,
				reciever      => $channel,
				sender        => $sender,
				message       => $message,
			}
		);

		push @{ $self->{chat_rooms}{$channel}{chat_log} },
			"$sender: $message\n";

		return $packet, \@{ $self->{chat_rooms}{$channel}{members} },
	}

}

sub private_message {    
	my ( $self, $message, $sender, $reciever ) = @_;

	# Create packet and send it to all the clients in the chat room
	my $packet = SM::Server::Packet::Out::Chat::Message->new(
		{   reciever_type => PRIVMSG,
			reciever      => $reciever,
			sender        => $sender,
			message       => $message,
		}
	);

	print Dumper($packet);
	return $packet;
}


1;
